/*
所有全局函数都在这里定义，已经被vue引用，使用时直接this.$global.方法名()调用
 */
import state from '../store/index'
import Vue from "vue";
import axios from "axios";

export default {
  test() {
    console.log("global test")
  },

  /**
   * 在页面刷新时，重新通过cookies导入信息
   */
  syncInfoOnRefresh() {
    state.commit('relogin')
  },


  /**
   * 转化成正格式的字符串
   * @param str
   * @returns {string}
   * @constructor
   */
  formatTimeString(str) {
    let date = new Date(Date.parse(str))
    return date.toLocaleString()
  },

  /**
   * 从制定的教学班id，课件id中下载文件，并命名为fileName
   * @param courseId
   * @param coursewareId
   * @param fileName
   */
  downloadCoursewareFile(courseId, coursewareId, fileName) {
    axios({
      method: 'get',
      url: '/api/downloadCoursewareFile',
      params: { courseId: courseId, coursewareId: coursewareId },
      responseType: 'blob',
    }).then(res1 => {
      console.log(res1)

      let url = window.URL.createObjectURL(res1.data)
      let link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      link.setAttribute('download', fileName)
      document.body.appendChild(link)
      link.click()
    })
  },

  /**
   * 将permission映射到字符串
   * @param dp
   * @returns {string}
   */
  mapDownloadPermission(dp) {
    const map = ['所有人可见可下载', '可见、不可下载', '学生不可见']
    return map[dp]
  },

  /**
   * 将开课学期的数字值映射到字符串
   * @param semNum
   * @returns {string}
   */
  mapSemester(semNum) {
    const lookup = ['全年', '春', '夏', '秋', '冬', '春夏', '秋冬', '暑学期']
    return lookup[semNum]
  },

  /**
   *  将schedule字符串映射为常见字符串
   * @param sched
   */
  mapSchedule(sched) {
    let topArr = sched.split(",")
    let rst = ""
    const dayLookup = ['一', '二', '三', '四', '五', '六', '日',]
    for (let i = 0; i < topArr.length; i++) {
      let arr = topArr[i].split(" ")
      rst += '周' + dayLookup[parseInt(arr[0])] + arr.slice(1).join("") + "节"
      if (i !== topArr.length - 1) rst += ', '
    }
    return rst
  },

  /**
   * 下载学生上传的作业附件
   * @param courseId
   * @param assignmentId
   * @param userEmail
   * @param fileName
   */
  downloadHwFile(courseId,assignmentId,userEmail,fileName){
    axios({
      method: 'get',
      url: '/api/downloadAssignmentAnswerFile',
      params: { courseId: courseId, assignmentId: assignmentId,userEmail:userEmail },
      responseType: 'blob',
    }).then(res1 => {
      console.log(res1)

      let url = window.URL.createObjectURL(res1.data)
      let link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      link.setAttribute('download', fileName)
      document.body.appendChild(link)
      link.click()
    })
  },


}
